package com.aisile.teacher.service.impl;


import com.aisile.model.teacher.dtos.TeacherAndStudentDto;
import com.aisile.model.teacher.dtos.TeacherListDto;
import com.aisile.model.student.pojos.Student;
import com.aisile.model.teacher.pojos.Teacher;
import com.aisile.model.base.vos.R;
import com.aisile.model.base.vos.RPage;
import com.aisile.teacher.feign.StudentFeign;
import com.aisile.teacher.mapper.TeacherMapper;
import com.aisile.teacher.service.TeacherService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Auther:yry
 * @Date:2024/6/21 0021
 * @VERSON:1.0
 */
@Service
public class TeacherServiceImpl extends ServiceImpl<TeacherMapper, Teacher> implements TeacherService {

    // 替换成远程调用
//    @Autowired
//    private StudentService studentService;
    @Autowired
    private StudentFeign studentFeign;

    @Override
    public R saveTeacherAndStudents(TeacherAndStudentDto dto) {
        // 先保存老师信息 mybatisPlus 框架 会把老师添加的id 回填给老师对象   tid关联
        Teacher teacher = dto.getTeacher();
        this.save(teacher);
        studentFeign.saveStudents(teacher.getId(),dto.getStudents());
        return R.ok();
    }

    @Override
    public R udpateTeacherAndStudents(TeacherAndStudentDto dto) {
        // 先保存老师信息 mybatisPlus 框架 会把老师添加的id 回填给老师对象   tid关联
        Integer tid = dto.getTeacher().getId();
        Teacher teacherOld = this.getById(tid);
        if(teacherOld == null){
            return R.fail("对象不存在");
        }
        this.updateById(dto.getTeacher());
        // 删除关联学生  然后再添加集合
        studentFeign.deleteStudentsbyTid(tid);
        studentFeign.saveStudents(tid,dto.getStudents());
        return R.ok();
    }

    @Override
    public R findList(TeacherListDto dto) {

        LambdaQueryWrapper<Teacher> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(dto.getShengid()!=null,Teacher::getShengid,dto.getShengid());
        lambdaQueryWrapper.eq(dto.getShiid()!=null,Teacher::getShiid,dto.getShiid());
        lambdaQueryWrapper.eq(dto.getQuid()!=null,Teacher::getQuid,dto.getQuid());
        lambdaQueryWrapper.eq(dto.getName()!=null && !"".equals(dto.getName()),Teacher::getName,dto.getName());



        IPage<Teacher> iPage = this.page(new Page<>(dto.getPage(),dto.getSize()),lambdaQueryWrapper);
        R r = new RPage(iPage.getTotal(),dto.getPage(),dto.getSize());
        r.setData(iPage.getRecords());
        return r;
    }

    @Override
    public R deleteTeacherAndStudent(Integer id) {
        // 先删除老师
        this.removeById(id);
        // 删除学生
        studentFeign.deleteStudentsbyTid(id);
        return R.ok();
    }

    @Override
    public R deleteTeacherByIds(List<Integer> ids) {
        for (Integer id:ids){
            deleteTeacherAndStudent(id);
        }
        return R.ok();
    }

    @Override
    public R findTeacherById(Integer id) {
        // 查询老师
        Teacher teacher =  this.getById(id);
        List<Student> students = studentFeign.getStudentByTid(id);

        Map<String,Object> resultMap = new HashMap<>();
        resultMap.put("teacher",teacher);
        resultMap.put("students",students);

        return R.ok(resultMap);
    }
}
